package com.l024.framework.view;

import android.animation.ValueAnimator;
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ScrollView;

public class HeadZoomScrollView extends ScrollView {

    //头部View
    private View mZoomView;
    private int mZoomViewWidth;
    private int mZoomViewHeight;
    //是否在滑动
    private boolean isScrolling;
    //第一次按住的坐标
    private float firstPosition;
    //滑动系数
    private float mScrollRate = 0.3f;
    //回弹系数
    private float mReplayRate = 0.5f;

    public HeadZoomScrollView(Context context) {
        super(context);
    }

    public HeadZoomScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public HeadZoomScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    /**
     * 1.获取头部View
     * 2.滑动事件
     */
    /**
     * 调用setContentView之后就会调用onFinishInflate这个方法，这个方法就代表自定义控件中的子控件映射完成了，然后可以进行一些初始化控件的操作
     * 比如： 可以通过findViewById 得到控件，得到控件之后进行一些初始化的操作（既然能够得到控件你就可以，随心所欲了撒）
     * ，就像在Activity中的onCreate方法里面一样，当然在这个方法里面是得不到控件的高宽的，
     * 控件的高宽是必须在调用了onMeasure方法之后才能得到，
     * 而onFinishInflate方法是在setContentView之后、onMeasure之前
     */
    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        if(getChildAt(0)!=null){
            ViewGroup vg = (ViewGroup) getChildAt(0);
            if(vg.getChildAt(0)!=null){
                mZoomView = vg.getChildAt(0);
            }
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        if(mZoomViewWidth<=0||mZoomViewHeight<=0){
            mZoomViewWidth = mZoomView.getMeasuredWidth();
            mZoomViewHeight = mZoomView.getMeasuredHeight();
        }
        switch (ev.getAction()){
            case MotionEvent.ACTION_MOVE:{
                if(!isScrolling){
                    if(getScrollY() == 0){
                        //没有滑动说明是第一次滑动，记录
                        firstPosition = ev.getY();
                    }else {
                        break;
                    }
                }
                //计算缩放值
                //公式 （当前位置-第一次按下位置)*缩放系数
                int distance = (int)((ev.getY()-firstPosition)*mScrollRate);
                if(distance<0){
                    break;
                }
                isScrolling = true;
                setZoomView(distance);
                break;
            }
            case MotionEvent.ACTION_UP:{
                replayZoomView();
                break;
            }
        }
        return true;
    }

    /**
     * 下拉缩放
     * @param zoom
     */
    private void setZoomView(float zoom){
        if(mZoomViewWidth<=0||mZoomViewHeight<=0){
            return;
        }
        ViewGroup.LayoutParams lp = mZoomView.getLayoutParams();
        lp.width = (int) (mZoomViewWidth+zoom);
        //现在的宽/以前的宽=缩放比例*现在的高
        lp.height = (int)(mZoomViewHeight*((mZoomViewWidth+zoom)/mZoomViewWidth));
        //设置间距
        //公司-(lp.width-mZoomViewWidth)/2
        ((MarginLayoutParams)lp).setMargins(-(lp.width-mZoomViewWidth)/2,0,0,0);
        mZoomView.setLayoutParams(lp);
    }

    /**
     * 回弹
     */
    private void replayZoomView(){
        //计算下拉的缩放值在让属性动画复原
        int distance = mZoomView.getMeasuredWidth()-mZoomViewWidth;
        ValueAnimator valueAnimator = ValueAnimator.ofFloat(distance,0).setDuration((long)(distance*mReplayRate));
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                setZoomView((Float)animation.getAnimatedValue());
            }
        });
        valueAnimator.start();
    }
}
